#include <stdio.h>
#include "QHDX_main.h"

void AddStudentInfo()
{
	printf("\n******欢迎来到增加模式******\n");
	while(1)
	{
		struct StudentInfo AddStudent;
		printf("\n请根据提示添加学生信息\n");
		while(1)
		{
			printf("\n请输入学生学号:>");
			scanf("%d", &AddStudent.number);
			
			if(Is_Student_Exist(AddStudent.number))
			{
				printf("该学生信息已经存在,请重新输入");
				while(getchar() != '\n');
				continue;
			}
			else  break;
		}
		
		printf("请输入学生姓名:>");
		scanf("%s", &AddStudent.name);

		printf("请输入学生性别(女生输入0,男生输入1):>");
		scanf("%hd", &AddStudent.sex);

		printf("请输入学生语文成绩:>");
		scanf("%hu", &AddStudent.grade.Chinese);

		printf("请输入学生数学成绩:>");
		scanf("%hu", &AddStudent.grade.Math);

		printf("请输入学生英语成绩:>");
		scanf("%hd", &AddStudent.grade.English);
		
		FILE* fp = fopen("StudentData", "ab");
		fwrite(&AddStudent, sizeof(AddStudent), 1, fp);
		fclose(fp);

		printf("成功增加学生信息！");
		printf("是否继续录入？(y/n)");
		getchar();
		if(getchar() != 'y') break;
	}
}

int Is_Student_Exist(int number)
{
	struct StudentInfo FindStudent;
	int flag = 0;
	FILE* fp = fopen("StudentData","a+b");
	if(fp == NULL) return 0;

	while(fread(&FindStudent, sizeof(FindStudent), 1, fp))
	{
		if(FindStudent.number == number)
		{
			flag = 1;
			break;
		}
	}
	fclose(fp);
	return flag;
}

